package be.kul.osgi.ds.socketcommunication;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import be.kul.osgi.ds.comminterface.CommunicationInterface;
import be.kul.osgi.ds.comminterface.MyEvent;
import be.kul.osgi.ds.comminterface.MyEventListener;


public class Sock implements CommunicationInterface
{
	DatagramSocket socket;
	//MulticastSocket ms;
    byte[] receiveData;
    byte[] sendData;
    String group; 
	int port;
	
	//listeners
	private List _listeners = new ArrayList();
	
    public Sock()
	{
    	System.out.println("Sock Object made");
		//this.port = 4567;
	}
	
	
	@Override
	public void initserver() {
		/*try {
			//ServerThread st = new ServerThread(this);
			//new Thread(st);
			//Thread t = new Thread(st);
			//t.start();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	*/
	}

	@Override
	public void init(int port) {
		this.port = port;
		try {
			socket = new DatagramSocket();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addToGroup(String group)
	{
		this.group = group;
	}
	
	public void send(String s, String IPAddress) 
	{
		InetAddress address;
		try {
			address = InetAddress.getByName(IPAddress);
			 sendData = s.getBytes();
	         DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
	         try {
	        	
	        	System.out.println("sended");
				socket.send(sendPacket);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
	}
	
	public void receive()
	{
		try {
			ServerThread st = new ServerThread(this);
			Thread t = new Thread(st);
			t.start();
			//ServerThreadUni st2 = new ServerThreadUni(this);
			//Thread t2 = new Thread(st2);
			//t2.start();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void sendmulticast(String s)
	{
		send(s, group);
	}
	
	public void messagereceived(String s, String sender, int sendersocket){
		_fireMyEvent(s, sender, sendersocket);
	}
	
	//eventlistening part
		public synchronized void addMyEventListener( MyEventListener l ) {
	        _listeners.add( l );
	    }
	    
	    public synchronized void removeMyEventListener( MyEventListener l ) {
	        _listeners.remove( l );
	    }
	       
	    public synchronized void _fireMyEvent(String m, String sender, int sendersocket)  {
	        MyEvent ev = new MyEvent( this , m, sender, sendersocket);
	        Iterator listeners = _listeners.iterator();
	        while( listeners.hasNext() ) {
	            ( (MyEventListener) listeners.next() ).messageReceived(ev);
	        }
	    }

}